<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<!-- HTML file produced from file: manual.tex --
 -- using Hyperlatex v 2.3.1 (c) Otfried Cheong--
 -- on Emacs 21.4 (patch 15) "Security Through Obscurity" XEmacs Lucid, Fri Dec  3 20:36:28 2004 -->
<HEAD>
<TITLE>Scheme 48 Manual -- Using Scheme records in C code</TITLE>

</HEAD><BODY BGCOLOR="#ffffff">
<EM>Scheme 48 Manual</EM> | <A HREF="s48manual.html#top_node">Contents</A> | In Chapter: <A HREF="s48manual_65.html">Mixing Scheme 48 and C</A><BR>Previous: <A HREF="s48manual_75.html">Using Scheme records in C code</A> | Next: <A HREF="s48manual_75.html">Using Scheme records in C code</A>
<H2>Using Scheme records in C code</H2>
<P>External modules can create records and access their slots
 positionally.
<P><UL><LI><table border=0 cellspacing=0 cellpadding=0 width=80%>
<tr> <td><CODE>s48_value s48_make_record(s48_value)</CODE></td> <td align=right>(may GC)</td></tr></table>
<LI><CODE>int       S48_RECORD_P(s48_value)</CODE> 
<LI><CODE>s48_value S48_RECORD_TYPE(s48_value)</CODE> 
<LI><CODE>s48_value S48_RECORD_REF(s48_value, long)</CODE> 
<LI><CODE>void      S48_RECORD_SET(s48_value, long, s48_value)</CODE> 
</UL>
The argument to <CODE>s48_make_record</CODE> should be a shared binding
 whose value is a record type.
In C the fields of Scheme records are only accessible via offsets,
 with the first field having offset zero, the second offset one, and
 so forth.
If the order of the fields is changed in the Scheme definition of the
 record type the C code must be updated as well.
<P>For example, given the following record-type definition
<BLOCKQUOTE><PRE>
(define-record-type thing :thing
  (make-thing a b)
  thing?
  (a thing-a)
  (b thing-b))
</PRE></BLOCKQUOTE>
the identifier <CODE>:thing</CODE> is bound to the record type and can
 be exported to C:
<BLOCKQUOTE><PRE>
(define-exported-binding "thing-record-type" :thing)
</PRE></BLOCKQUOTE>
<CODE>Thing</CODE> records can then be made in C:
<BLOCKQUOTE><PRE>
static s48_value
  thing_record_type_binding = S48_FALSE;

void initialize_things(void)
{
  S48_GC_PROTECT_GLOBAL(thing_record_type_binding);
  thing_record_type_binding =
     s48_get_imported_binding("thing-record-type");
}

s48_value make_thing(s48_value a, s48_value b)
{
  s48_value thing;
  s48_DECLARE_GC_PROTECT(2);

  S48_GC_PROTECT_2(a, b);

  thing = s48_make_record(thing_record_type_binding);
  S48_RECORD_SET(thing, 0, a);
  S48_RECORD_SET(thing, 1, b);

  S48_GC_UNPROTECT();

  return thing;
}
</PRE></BLOCKQUOTE>
Note that the variables <CODE>a</CODE> and <CODE>b</CODE> must be protected
 against the possibility of a garbage collection occuring during
 the call to <CODE>s48_make_record()</CODE>.
<P><P>
  
Previous: <A HREF="s48manual_75.html">Using Scheme records in C code</A> | Next: <A HREF="s48manual_75.html">Using Scheme records in C code</A></BODY></HTML>
